package cn.lyq.card.ui.statistics

import android.content.Context
import android.content.Intent
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.utils.bindViewPager
import cn.lyq.card.utils.setSimpleData
import kotlinx.android.synthetic.main.act_dev_proxy_detail.*
import kotlinx.android.synthetic.main.layout_tab_normal.*

class DevProxyDetailActivity : BaseActivity() {

    companion object {
        private const val KEY_ID = "ID"
        private const val KEY_TEAM = "TEAM"
        private const val KEY_INDEX = "INDEX"
        private const val KEY_AGENT = "AGENT"

        @JvmStatic
        fun start(context: Context, id: Int, index: Int, team: Int = 0, isAgent: Boolean = false) {
            val starter = Intent(context, DevProxyDetailActivity::class.java)
                .putExtra(KEY_ID, id)
                .putExtra(KEY_TEAM, team)
                .putExtra(KEY_INDEX, index)
                .putExtra(KEY_AGENT, isAgent)
            context.startActivity(starter)
        }
    }

    private val mModel by lazy {
        getVM(StatisticsVM::class.java)
    }
    private val mPageAdapter by lazy {
        PagerAdapter(this)
    }

    override fun getLayoutId(): Int {
        return R.layout.act_dev_proxy_detail
    }

    override fun initData() {
        super.initData()
        if (intent.getBooleanExtra(KEY_AGENT, false)) {
            mModel.agentId = intent.getIntExtra(KEY_ID, -1)
        } else {
            mModel.goodsId = intent.getIntExtra(KEY_ID, -1)
        }
        mModel.team = intent.getIntExtra(KEY_TEAM, 0)
    }

    override fun initView() {
        super.initView()
        setMyTitle(R.string.str_dev_act_detail)

        val tabTitles = arrayOf("已激活", "未激活")
        tabLayout.setSimpleData(tabTitles)
        viewPager.adapter = mPageAdapter
        tabLayout.setSimpleData(tabTitles)
        tabLayout.bindViewPager(viewPager)

        if (intent.getIntExtra(KEY_INDEX, 0) == 1) {
            viewPager.setCurrentItem(1, false)
        }
    }

    private class PagerAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {

        override fun getItemCount(): Int = 2

        override fun createFragment(position: Int): DevProxyDetailFg {
            return DevProxyDetailFg.newInstance(position)
        }

    }
}